package com.example.api.controller.admin;

import com.example.api.constant.StatusConst;
import com.example.api.dto.CategoryDTO;
import com.example.api.exception.ParamException;
import com.example.api.query.CategoryQuery;
import com.example.api.service.CategoryService;
import com.example.api.utils.PageResult;
import com.example.api.utils.Result;
import com.example.api.vo.CategoryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "菜品分类接口")
@RestController
@RequestMapping("/admin/category")
public class CategoryController {

    @Resource
    CategoryService categoryService;

    @ApiOperation("添加菜品分类")
    @PostMapping
    public Result add(CategoryDTO categoryDTO){
        categoryService.add(categoryDTO);
        return Result.ok("添加成功");
    }

    @ApiOperation("编辑菜品分类")
    @PutMapping("/{id}")
    public Result update(@PathVariable long id,CategoryDTO categoryDTO){ //@RequestBody将前端传递的json格式的请求参数转为对象
        categoryService.update(id,categoryDTO);
        return Result.ok("修改成功");
    }

    @ApiOperation("禁用启动菜品分类")
    @PutMapping("/{id}/status")
    public Result updateStatus(@PathVariable long id,String status){
        if(StatusConst.INACTIVE_STATUS_NAME.equals(status)){
            categoryService.updateStatus(id,StatusConst.INACTIVE_STATUS);
        }else if(StatusConst.ACTIVE_STATUS_NAME.equals(status)){
            categoryService.updateStatus(id,StatusConst.ACTIVE_STATUS);
        }else{
            throw new ParamException("参数错误");
        }
        return Result.ok("修改状态成功");
    }


    @ApiOperation("查询菜品分类")
    @GetMapping
    public Result<List<CategoryVO>> find(CategoryQuery categoryQuery){ //CategoryQuery封装了查询的条件
        PageResult<CategoryVO> page = categoryService.find(categoryQuery);

        return Result.ok("成功",page.getList(),page.getTotal());
    }

    @ApiOperation("删除菜品分类")
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable long id){
        categoryService.deleteById(id);
        return Result.ok("删除成功");
    }
}
